#!/usr/bin/env python3
#coding=utf-8

import os
import sys
from distutils.core import setup
from distutils.command.build_scripts import build_scripts


def execDir():
    '''執行檔所在資料夾'''
    return os.path.abspath(os.path.dirname(sys.argv[0]))
    # return os.path.dirname(os.path.realpath(__file__))

# source pack library change
src_lib_dir = os.path.join(execDir(), 'lonotelib')
sys.path[0] = src_lib_dir


import lonote.info

def collect_data_files():
    data_files = [
        ('share/applications', ['desktop/lonote.desktop']),
        ('share/pixmaps', ['desktop/lonote.png']),
        ('share/pixmaps', ['desktop/lonote.ico'])
        ]

    # server data
    for root_dir in ['server']:
        for dir, dirs, files in os.walk(root_dir):
            target = os.path.join('share', 'lonote', dir)
            if files:
                files = [os.path.join(dir, f) for f in files]
                data_files.append((target, files))

    # i18n
    for dir, dirs, files in os.walk('locale'):
        target = os.path.join('share', dir)
        if files:
            files = [os.path.join(dir, f) for f in files]
            data_files.append((target, files))

    # config
    data_files.extend([
        os.path.join('config', 'lonote', 'book', '.hgempty'),
        os.path.join('config', 'lonote', 'index', '.hgempty'),
        os.path.join('config', 'lonote', 'static', '.hgempty'),
        ])

    # docs
    source_docdir = os.path.join('doc')
    for dir, dirs, files in os.walk(source_docdir):
        target = os.path.join('share', 'doc', 'lonote')
        if files:
            files = [os.path.join(dir, f) for f in files]
            data_files.append((target, files))

    # docs: Win
    # source_windocdir = os.path.join('doc', 'Win')
    # for dir, dirs, files in os.walk(source_windocdir):
    #     if files:
    #         files = [os.path.join(dir, f) for f in files]
    #         data_files.extend(files)

    # man
    unix_manual_dir = os.path.join('doc', 'man')
    for dir, dirs, files in os.walk(unix_manual_dir):
        subdir = os.path.relpath(dir, 'doc')
        target = os.path.join('share', subdir)
        if files:
            files = [os.path.join(dir, f) for f in files]
            data_files.append((target, files))

    # other_setup_script
    data_files.extend([
        'setup_cxfreeze_win.py',
        ])


    return data_files


class lonote_build_scripts(build_scripts):
	'''在 POSIX 中，lonote.py 重命名至 lonote'''

	def run(self):
		build_scripts.run(self)
		if os.name == 'posix' and not self.dry_run:
                    file = os.path.join(self.build_dir, 'lonote.py')
                    print('renaming %s to %s' % (file, file[:-3]))
                    os.rename(file, file[:-3]) # len('.py') == 4


setup(name='lonote',
      version = lonote.info.version,
      description = lonote.info.desc_long,
      author = lonote.info.designer,
      author_email = lonote.info.designer_mail,
      url = lonote.info.homepage,
      download_url = lonote.info.download_url,
      classifiers=[
        'Environment :: Web Environment',
        'Intended Audience :: End Users/Desktop',
        'License :: OSI Approved :: Apache Software License',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: POSIX :: Linux',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.2',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: JavaScript',
        'Topic :: Internet :: WWW/HTTP :: WSGI',
        'Topic :: Text Editors'
        ],
      scripts=['lonote.py'],
      py_modules=[ 'diff_match_patch',
          'bottle',
          'lonote.translate'],
      package_dir = {'': 'lonotelib'},
      packages=['lonote',
          'civa',
          'whoosh',
          'whoosh.codec',
          'whoosh.filedb',
          'whoosh.lang',
          'whoosh.matching',
          'whoosh.qparser',
          'whoosh.query',
          'whoosh.support'],
      data_files = collect_data_files(),
      cmdclass = {
		'build_scripts': lonote_build_scripts,
	},
     )
